package com.example.recyclerview

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.recyclerview.databinding.ItemPostBinding

/**
 * @author yaotianxue
 * @description
 * @date 2024/10/9
 */
class PostAdapter(
    private val imageClick:(()->Unit)? = null,
    private val onLikeClick:((position:Int)->Unit)? = null
):RecyclerView.Adapter<PostAdapter.PostViewHolder>() {
    companion object{
        private const val TAG = "PostAdapter"
    }
    private val list = mutableListOf<PostEntity>()

    fun updateData(position:Int){
        list[position].like = !list[position].like
        notifyItemChanged(position,"like")
    }

    fun removeData(position: Int){
        list.removeAt(position)
        notifyItemRemoved(position)

    }
     fun addData(postEntity: PostEntity){
        list.add(postEntity)
        notifyItemInserted(list.size -1 )
    }
    /**
     * 追加数据
     */
    fun addData(data:MutableList<PostEntity>){
        val startPosition = list.size
        list.addAll(data)
        notifyItemRangeInserted(startPosition,data.size)//从哪个位置开始添加多少个
    }

    /**
     * 设置新的数据源
     */
    fun setData(data:MutableList<PostEntity>){
        list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return list.size

    }
    /**
     * 创建viewHolder 执行几次 200次
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        Log.d(TAG, "onCreateViewHolder: ")
        val layoutInflater = LayoutInflater.from(parent.context)
        val postViewHolder = PostViewHolder(binding = ItemPostBinding.inflate(layoutInflater, parent, false),imageClick = imageClick,onLikeClick = onLikeClick)
        return postViewHolder
    }

    /**
     * 绑定viewHolder 执行几次 200次
     */
    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        holder.binding.holder = holder
        holder.initView(list[position]) //绑定数据封装在PostViewHolder
        Log.d(TAG, "onBindViewHolder: $position")
//        //图片点击会产生大量匿名对象
//        holder.binding.iv.setOnClickListener {
//
//        }
    }

    /**
     * 局部增量刷新
     */
    override fun onBindViewHolder(
        holder: PostViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        //payloads为空执行默认，不为空，局部增量刷新
        if(payloads.isEmpty()){
           onBindViewHolder(holder,position)
        }else{
            if(payloads[0] == "like"){
                holder.initLikeView(list[position])
            }
        }

    }

    inner class PostViewHolder(val binding:ItemPostBinding, private val imageClick:(()->Unit)? = null,private val onLikeClick:((position:Int)->Unit)? = null):RecyclerView.ViewHolder(binding.root){

        fun clickImage(){
            imageClick?.invoke() //回调点击事件
        }
        fun clickLikeImage(){
            onLikeClick?.invoke(this.adapterPosition) //回调点击事件
        }

        /**
         * 绑定数据
         */
        fun initView(postEntity: PostEntity){
            binding.tv.text = list[position].title
            Glide.with(binding.iv).load(list[position].image).into(binding.iv)
            initLikeView(postEntity)
        }

        /**
         * 只刷新爱心UI
         */
        fun initLikeView(postEntity: PostEntity){
            if(postEntity.like){
                binding.ivLike.setImageResource(R.drawable.icon_like)
            }else{
                binding.ivLike.setImageResource(R.drawable.icon_like_no)
            }
        }

    }
}